/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.DistributionList;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Group;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Provisioning.GroupMembership;
/*     */ import com.zimbra.cs.account.ldap.LdapProvisioning;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.junit.Assert;
/*     */ 
/*     */ public class TestGroups extends junit.framework.TestCase
/*     */ {
/*     */   final String domainName = "tgcache.test";
/*     */   final String domainName1 = "tgcache1.test";
/*     */   final String domainName2 = "tgcache2.test";
/*     */   final String acctPatt = "person%03d@tgcache.test";
/*     */   final String acctWithAlias = "tgacctwithalias@tgcache.test";
/*     */   final String acctAlias = "tgacctalias@tgcache.test";
/*     */   final String acctWithAlias2 = "tgacctwithalias2@tgcache.test";
/*     */   final String acctAlias2 = "tgacctalias2@tgcache.test";
/*     */   final String dlWithAlias = "tgdlwithalias@tgcache.test";
/*     */   final String dlAlias = "tgdlalias@tgcache.test";
/*     */   final String dlWithAlias2 = "tgdlwithalias2@tgcache.test";
/*     */   final String dlAlias2 = "tgdlalias2@tgcache.test";
/*     */   final String normalDLPatt = "normaldl%03d@tgcache.test";
/*     */   final String dynamicDLPatt = "dynamicdl%03d@tgcache.test";
/*     */   final String customDLPatt = "cosdl%03d@tgcache.test";
/*     */   final String cosPatt = "cdgcachecos%03d";
/*     */   final int NUM_ACCOUNTS_SMALL = 8;
/*     */   final int NUM_NORMAL_DL_SMALL = 8;
/*     */   final int NUM_DYNAMIC_DL_SMALL = 8;
/*     */   final int NUM_ACCOUNTS_PERF = 40;
/*     */   final int NUM_NORMAL_DL_PERF = 1000;
/*     */   final int NUM_DYNAMIC_DL_PERF = 200;
/*     */   final int NUM_ACCOUNTS = 8;
/*     */   final int NUM_NORMAL_DL = 8;
/*     */   final int NUM_DYNAMIC_DL = 8;
/*     */   final int NUM_COS = 4;
/*     */   final int NUM_CUSTOM_DL = 4;
/*     */   private Domain domain;
/*     */   private Domain domain1;
/*     */   private Domain domain2;
/*     */   private com.zimbra.cs.account.soap.SoapProvisioning soapProv;
/*     */   private LdapProvisioning ldapProv;
/*     */   private final Map<String, Group> groups;
/*     */   
/*     */   public TestGroups()
/*     */   {
/*  54 */     this.domainName = "tgcache.test";
/*  55 */     this.domainName1 = "tgcache1.test";
/*  56 */     this.domainName2 = "tgcache2.test";
/*  57 */     this.acctPatt = "person%03d@tgcache.test";
/*  58 */     this.acctWithAlias = "tgacctwithalias@tgcache.test";
/*  59 */     this.acctAlias = "tgacctalias@tgcache.test";
/*  60 */     this.acctWithAlias2 = "tgacctwithalias2@tgcache.test";
/*  61 */     this.acctAlias2 = "tgacctalias2@tgcache.test";
/*  62 */     this.dlWithAlias = "tgdlwithalias@tgcache.test";
/*  63 */     this.dlAlias = "tgdlalias@tgcache.test";
/*  64 */     this.dlWithAlias2 = "tgdlwithalias2@tgcache.test";
/*  65 */     this.dlAlias2 = "tgdlalias2@tgcache.test";
/*  66 */     this.normalDLPatt = "normaldl%03d@tgcache.test";
/*  67 */     this.dynamicDLPatt = "dynamicdl%03d@tgcache.test";
/*  68 */     this.customDLPatt = "cosdl%03d@tgcache.test";
/*  69 */     this.cosPatt = "cdgcachecos%03d";
/*     */     
/*  71 */     this.NUM_ACCOUNTS_SMALL = 8;
/*  72 */     this.NUM_NORMAL_DL_SMALL = 8;
/*  73 */     this.NUM_DYNAMIC_DL_SMALL = 8;
/*     */     
/*  75 */     this.NUM_ACCOUNTS_PERF = 40;
/*  76 */     this.NUM_NORMAL_DL_PERF = 1000;
/*  77 */     this.NUM_DYNAMIC_DL_PERF = 200;
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*  82 */     this.NUM_ACCOUNTS = 8;
/*  83 */     this.NUM_NORMAL_DL = 8;
/*  84 */     this.NUM_DYNAMIC_DL = 8;
/*     */     
/*  86 */     this.NUM_COS = 4;
/*  87 */     this.NUM_CUSTOM_DL = 4;
/*     */     
/*  89 */     this.domain = null;
/*  90 */     this.domain1 = null;
/*  91 */     this.domain2 = null;
/*  92 */     this.soapProv = null;
/*  93 */     this.ldapProv = null;
/*  94 */     this.groups = com.google.common.collect.Maps.newHashMap();
/*     */   }
/*     */   
/*     */   private class GetMembershipClientThread implements Runnable
/*     */   {
/*     */     private final Account acct;
/*     */     private final LdapProvisioning prov;
/*     */     private final java.util.Set<com.zimbra.cs.account.accesscontrol.Right> urights;
/*     */     
/*     */     private GetMembershipClientThread(Account p, java.util.Set<com.zimbra.cs.account.accesscontrol.Right> account) {
/* 104 */       this.prov = p;
/* 105 */       this.acct = account;
/* 106 */       this.urights = rights;
/*     */     }
/*     */     
/*     */ 
/*     */     public void run()
/*     */     {
/*     */       try
/*     */       {
/* 114 */         for (int cnt = 0; cnt < 20; cnt++) {
/* 115 */           long before = System.currentTimeMillis();
/* 116 */           Provisioning.GroupMembership membership = this.prov.getGroupMembership(this.acct, false);
/* 117 */           ZimbraLog.test.info("XXX getGroupMembership ms=%s acct=%s NUM=%s", new Object[] { ZimbraLog.elapsedTime(before, System.currentTimeMillis()), this.acct.getName(), Integer.valueOf(membership.groupIds().size()) });
/*     */           
/*     */ 
/* 120 */           before = System.currentTimeMillis();
/* 121 */           membership = this.prov.getGroupMembershipWithRights(this.acct, this.urights, false);
/* 122 */           ZimbraLog.test.info("XXX getGroupMembershipWithRights ms=%s acct=%s NUM=%s", new Object[] { ZimbraLog.elapsedTime(before, System.currentTimeMillis()), this.acct.getName(), Integer.valueOf(membership.groupIds().size()) });
/*     */         }
/*     */       }
/*     */       catch (Exception e)
/*     */       {
/* 127 */         ZimbraLog.test.error("Unable to get membership for %s.", this.acct, e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public void ENABLE_FOR_PERFORMANCE_TESTStestCustomDynamicGroups()
/*     */     throws Exception
/*     */   {
/* 136 */     long start = System.currentTimeMillis();
/* 137 */     com.zimbra.cs.account.accesscontrol.RightManager rightMgr = com.zimbra.cs.account.accesscontrol.RightManager.getInstance();
/* 138 */     java.util.Set<com.zimbra.cs.account.accesscontrol.Right> rights = com.google.common.collect.Sets.newHashSet();
/* 139 */     rights.add(rightMgr.getUserRight("createDistList"));
/* 140 */     Thread[] threads = new Thread[80];
/* 141 */     for (int i = 0; i < threads.length; i++) {
/* 142 */       String acctName = String.format("person%03d@tgcache.test", new Object[] { Integer.valueOf(i % 10 + 1) });
/* 143 */       Account acct = this.soapProv.getAccountByName(acctName);
/* 144 */       threads[i] = new Thread(new GetMembershipClientThread(this.ldapProv, acct, rights, null));
/*     */     }
/* 146 */     for (Thread thread : threads) {
/* 147 */       thread.start();
/*     */     }
/* 149 */     for (Thread thread : threads) {
/* 150 */       thread.join();
/*     */     }
/* 152 */     ZimbraLog.test.info("ZZZ testCustomDynamicGroups %s", new Object[] { ZimbraLog.elapsedTime(start, System.currentTimeMillis()) });
/*     */   }
/*     */   
/*     */   private Provisioning.GroupMembership doGetGroupMembershipWithRights(Account acct, java.util.Set<com.zimbra.cs.account.accesscontrol.Right> rights, int expected, int adminOnlyExpected) throws com.zimbra.common.service.ServiceException
/*     */   {
/*     */     String rightsDesc;
/*     */     String rightsDesc;
/* 159 */     if (rights == null) {
/* 160 */       rightsDesc = "rights={ALL(null)}"; } else { String rightsDesc;
/* 161 */       if (rights.isEmpty()) {
/* 162 */         rightsDesc = "rights={ALL(empty)}";
/*     */       } else {
/* 164 */         StringBuilder sb = new StringBuilder("rights=");
/* 165 */         for (com.zimbra.cs.account.accesscontrol.Right right : rights) {
/* 166 */           if (sb.length() > 7) {
/* 167 */             sb.append(',');
/*     */           }
/* 169 */           sb.append(right.getName());
/*     */         }
/* 171 */         rightsDesc = sb.toString();
/*     */       } }
/* 173 */     long before = System.currentTimeMillis();
/* 174 */     Provisioning.GroupMembership membership = this.ldapProv.getGroupMembershipWithRights(acct, rights, false);
/* 175 */     long after = System.currentTimeMillis();
/* 176 */     String groupNames = groupInfo(membership.groupIds());
/* 177 */     ZimbraLog.test.info("YYY getGroupMembershipWithRights %s ms=%s acct=%s size=%s\n%s\ngroupNames=%s", new Object[] { rightsDesc, Long.valueOf(after - before), acct.getName(), Integer.valueOf(membership.groupIds().size()), membership, groupNames });
/*     */     
/*     */ 
/* 180 */     before = System.currentTimeMillis();
/* 181 */     Provisioning.GroupMembership adminOnlyMembership = this.ldapProv.getGroupMembershipWithRights(acct, rights, true);
/* 182 */     after = System.currentTimeMillis();
/* 183 */     String adminOnlyGroupNames = groupInfo(membership.groupIds());
/* 184 */     ZimbraLog.test.info("YYY getGroupMembershipWithRights [adminOnly] %s ms=%s acct=%s size=%s\n%s\ngroupNames=%s", new Object[] { rightsDesc, Long.valueOf(after - before), acct.getName(), Integer.valueOf(adminOnlyMembership.groupIds().size()), adminOnlyMembership, adminOnlyGroupNames });
/*     */     
/*     */ 
/*     */ 
/* 188 */     assertEquals(String.format("Number of groups with %s which contain %s groups=%s", new Object[] { rightsDesc, acct.getName(), groupNames }), expected, membership.groupIds().size());
/*     */     
/*     */ 
/* 191 */     assertEquals(String.format("Number of adminOnly groups with %s which contain %s groups=%s", new Object[] { rightsDesc, acct.getName(), adminOnlyGroupNames }), adminOnlyExpected, adminOnlyMembership.groupIds().size());
/*     */     
/*     */ 
/* 194 */     return membership;
/*     */   }
/*     */   
/*     */   private String groupInfo(Iterable<String> groupIDs) {
/* 198 */     StringBuilder sb = new StringBuilder();
/* 199 */     for (String groupID : groupIDs) {
/* 200 */       if (sb.length() > 0) {
/* 201 */         sb.append(',');
/*     */       }
/* 203 */       Group grp = (Group)this.groups.get(groupID);
/* 204 */       if (grp == null) {
/* 205 */         sb.append("UNKNOWN (not created by this test):").append(groupID);
/*     */       } else {
/* 207 */         sb.append(grp.getName()).append("(id=").append(groupID).append(")");
/*     */       }
/*     */     }
/* 210 */     return sb.toString();
/*     */   }
/*     */   
/*     */   private void doAnyRightsTestForAccount(String acctName, int expected, int adminOnlyExpected) throws com.zimbra.common.service.ServiceException
/*     */   {
/* 215 */     java.util.Set<com.zimbra.cs.account.accesscontrol.Right> rights = com.google.common.collect.Sets.newHashSet();
/*     */     
/* 217 */     Account acct = this.soapProv.getAccountByName(acctName);
/* 218 */     doGetGroupMembershipWithRights(acct, rights, expected, adminOnlyExpected);
/* 219 */     doGetGroupMembershipWithRights(acct, null, expected, adminOnlyExpected);
/*     */   }
/*     */   
/*     */   private void doRightsTestForAccount(String acctName, int expected, int adminOnlyExpected) throws com.zimbra.common.service.ServiceException {
/* 223 */     com.zimbra.cs.account.accesscontrol.RightManager rightMgr = com.zimbra.cs.account.accesscontrol.RightManager.getInstance();
/* 224 */     java.util.Set<com.zimbra.cs.account.accesscontrol.Right> rights = com.google.common.collect.Sets.newHashSet();
/* 225 */     rights.add(rightMgr.getUserRight("createDistList"));
/*     */     
/* 227 */     Account acct = this.soapProv.getAccountByName(acctName);
/* 228 */     doGetGroupMembershipWithRights(acct, rights, expected, adminOnlyExpected);
/*     */   }
/*     */   
/*     */   public void testCustomDynamicGroupsAnyRights1() throws Exception {
/* 232 */     long start = System.currentTimeMillis();
/*     */     
/* 234 */     doAnyRightsTestForAccount(String.format("person%03d@tgcache.test", new Object[] { Integer.valueOf(1) }), 0, 0);
/* 235 */     ZimbraLog.test.info("ZZZ testCustomDynamicGroupsAnyRights1 %s", new Object[] { ZimbraLog.elapsedTime(start, System.currentTimeMillis()) });
/*     */   }
/*     */   
/*     */   public void testCustomDynamicGroupsCreateDistListRights1() throws Exception {
/* 239 */     long start = System.currentTimeMillis();
/* 240 */     doRightsTestForAccount(String.format("person%03d@tgcache.test", new Object[] { Integer.valueOf(1) }), 0, 0);
/* 241 */     ZimbraLog.test.info("ZZZ testCustomDynamicGroupsCreateDistListRights1 %s", new Object[] { ZimbraLog.elapsedTime(start, System.currentTimeMillis()) });
/*     */   }
/*     */   
/*     */   public void testCustomDynamicGroupsAnyRights2() throws Exception {
/* 245 */     long start = System.currentTimeMillis();
/*     */     
/*     */ 
/* 248 */     doAnyRightsTestForAccount(String.format("person%03d@tgcache.test", new Object[] { Integer.valueOf(2) }), 1, 0);
/* 249 */     ZimbraLog.test.info("ZZZ testCustomDynamicGroupsAnyRights2 %s", new Object[] { ZimbraLog.elapsedTime(start, System.currentTimeMillis()) });
/*     */   }
/*     */   
/*     */   public void testCustomDynamicGroupsCreateDistListRights2() throws Exception {
/* 253 */     long start = System.currentTimeMillis();
/* 254 */     doRightsTestForAccount(String.format("person%03d@tgcache.test", new Object[] { Integer.valueOf(2) }), 1, 0);
/* 255 */     ZimbraLog.test.info("ZZZ testCustomDynamicGroupsCreateDistListRights2 %s", new Object[] { ZimbraLog.elapsedTime(start, System.currentTimeMillis()) });
/*     */   }
/*     */   
/*     */   public void testCustomDynamicGroupsAnyRights3() throws Exception {
/* 259 */     long start = System.currentTimeMillis();
/*     */     
/*     */ 
/*     */ 
/* 263 */     doAnyRightsTestForAccount(String.format("person%03d@tgcache.test", new Object[] { Integer.valueOf(3) }), 2, 0);
/* 264 */     ZimbraLog.test.info("ZZZ testCustomDynamicGroupsCreateDistListRights3 %s", new Object[] { ZimbraLog.elapsedTime(start, System.currentTimeMillis()) });
/*     */   }
/*     */   
/*     */   public void testCustomDynamicGroupsCreateDistListRights3() throws Exception {
/* 268 */     long start = System.currentTimeMillis();
/* 269 */     doRightsTestForAccount(String.format("person%03d@tgcache.test", new Object[] { Integer.valueOf(3) }), 1, 0);
/* 270 */     ZimbraLog.test.info("ZZZ testCustomDynamicGroupsCreateDistListRights3 %s", new Object[] { ZimbraLog.elapsedTime(start, System.currentTimeMillis()) });
/*     */   }
/*     */   
/*     */   public void testCustomDynamicGroupsAnyRights4() throws Exception {
/* 274 */     long start = System.currentTimeMillis();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 279 */     doAnyRightsTestForAccount(String.format("person%03d@tgcache.test", new Object[] { Integer.valueOf(4) }), 2, 0);
/* 280 */     ZimbraLog.test.info("ZZZ testCustomDynamicGroupsCreateDistListRights4 %s", new Object[] { ZimbraLog.elapsedTime(start, System.currentTimeMillis()) });
/*     */   }
/*     */   
/*     */   public void testCustomDynamicGroupsCreateDistListRights4() throws Exception {
/* 284 */     long start = System.currentTimeMillis();
/* 285 */     doRightsTestForAccount(String.format("person%03d@tgcache.test", new Object[] { Integer.valueOf(4) }), 2, 0);
/* 286 */     ZimbraLog.test.info("ZZZ testCustomDynamicGroupsCreateDistListRights4 %s", new Object[] { ZimbraLog.elapsedTime(start, System.currentTimeMillis()) });
/*     */   }
/*     */   
/*     */   private void doGetCustomDynamicGroupMembership(int acctNum)
/*     */     throws com.zimbra.common.service.ServiceException
/*     */   {
/* 292 */     String acctName = String.format("person%03d@tgcache.test", new Object[] { Integer.valueOf(acctNum) });
/* 293 */     Account acct = this.ldapProv.getAccountByName(acctName);
/* 294 */     Provisioning.GroupMembership membership = this.ldapProv.getCustomDynamicGroupMembership(acct, false);
/* 295 */     String groupNames = groupInfo(membership.groupIds());
/* 296 */     assertEquals(String.format("Number of dynamic groups with custom memberURL s which contain %s groups=%s", new Object[] { acct.getName(), groupNames }), 1, membership.groupIds().size());
/*     */     
/* 298 */     String cosName = String.format("cosdl%03d@tgcache.test", new Object[] { Integer.valueOf(acctNum % 4 + 1) });
/* 299 */     Group grp = (Group)this.groups.get(membership.groupIds().get(0));
/* 300 */     String groupName = grp == null ? "UNKNOWN(not created by this test)" : grp.getName();
/* 301 */     assertEquals(String.format("Name of dynamic group with custom memberURL s which contains %s", new Object[] { acctName }), cosName, groupName);
/*     */   }
/*     */   
/*     */   public void testGetCustomDynamicGroups() throws Exception
/*     */   {
/* 306 */     long start = System.currentTimeMillis();
/* 307 */     doGetCustomDynamicGroupMembership(1);
/* 308 */     doGetCustomDynamicGroupMembership(4);
/* 309 */     ZimbraLog.test.info("ZZZ testGetCustomDynamicGroups %s", new Object[] { ZimbraLog.elapsedTime(start, System.currentTimeMillis()) });
/*     */   }
/*     */   
/*     */   public void testDLupdateGroupMembershipWithoutViaWithAliases() throws Exception
/*     */   {
/* 314 */     Account acct = this.ldapProv.getAccountByName("tgacctwithalias@tgcache.test");
/* 315 */     Provisioning.GroupMembership membership = new Provisioning.GroupMembership();
/* 316 */     long start = System.currentTimeMillis();
/* 317 */     DistributionList.updateGroupMembership(this.ldapProv, (com.zimbra.cs.ldap.ZLdapContext)null, membership, acct, null, false, false);
/*     */     
/* 319 */     ZimbraLog.test.info("testDLupdateGroupMembershipWithoutVia %s size=%d", new Object[] { ZimbraLog.elapsedTime(start, System.currentTimeMillis()), Integer.valueOf(membership.groupIds().size()) });
/*     */     
/* 321 */     for (int cnt = 1; cnt <= 8; cnt++) {
/* 322 */       String nam = String.format("normaldl%03d@tgcache.test", new Object[] { Integer.valueOf(cnt) });
/* 323 */       DistributionList dl = this.ldapProv.get(com.zimbra.common.account.Key.DistributionListBy.name, nam);
/* 324 */       Assert.assertTrue(String.format("DL %s (id=%s) in membership", new Object[] { nam, dl.getId() }), membership.groupIds().contains(dl.getId()));
/*     */     }
/*     */     
/* 327 */     DistributionList dl = this.ldapProv.get(com.zimbra.common.account.Key.DistributionListBy.name, "tgdlwithalias@tgcache.test");
/* 328 */     Assert.assertTrue(String.format("DL %s (id=%s) in membership", new Object[] { dl.getId(), dl.getId() }), membership.groupIds().contains(dl.getId()));
/*     */     
/* 330 */     dl = this.ldapProv.get(com.zimbra.common.account.Key.DistributionListBy.name, "tgdlwithalias2@tgcache.test");
/* 331 */     Assert.assertTrue(String.format("DL %s (id=%s) in membership", new Object[] { dl.getId(), dl.getId() }), membership.groupIds().contains(dl.getId()));
/*     */     
/* 333 */     Assert.assertEquals(String.format("Number of DLs User %s is a member of", new Object[] { "tgacctwithalias@tgcache.test" }), 10L, membership.memberOf().size());
/*     */     
/*     */ 
/* 336 */     acct = this.ldapProv.getAccountByName("tgacctwithalias2@tgcache.test");
/* 337 */     membership = new Provisioning.GroupMembership();
/* 338 */     start = System.currentTimeMillis();
/* 339 */     DistributionList.updateGroupMembership(this.ldapProv, (com.zimbra.cs.ldap.ZLdapContext)null, membership, acct, null, false, false);
/*     */     
/* 341 */     ZimbraLog.test.info("testDLupdateGroupMembershipWithoutVia %s size=%d", new Object[] { ZimbraLog.elapsedTime(start, System.currentTimeMillis()), Integer.valueOf(membership.groupIds().size()) });
/*     */     
/* 343 */     dl = this.ldapProv.get(com.zimbra.common.account.Key.DistributionListBy.name, "tgdlwithalias@tgcache.test");
/* 344 */     Assert.assertTrue(String.format("DL %s (id=%s) in membership", new Object[] { dl.getName(), dl.getId() }), membership.groupIds().contains(dl.getId()));
/*     */     
/* 346 */     dl = this.ldapProv.get(com.zimbra.common.account.Key.DistributionListBy.name, "tgdlwithalias2@tgcache.test");
/* 347 */     Assert.assertTrue(String.format("DL %s (id=%s) in membership", new Object[] { dl.getName(), dl.getId() }), membership.groupIds().contains(dl.getId()));
/*     */     
/* 349 */     Assert.assertEquals(String.format("Number of DLs User %s is a member of", new Object[] { "tgacctwithalias2@tgcache.test" }), 2L, membership.memberOf().size());
/*     */   }
/*     */   
/*     */   public void testDLupdateGroupMembershipWithViaWithAliases() throws Exception
/*     */   {
/* 354 */     Account acct = this.ldapProv.getAccountByName("tgacctwithalias@tgcache.test");
/* 355 */     Provisioning.GroupMembership membership = new Provisioning.GroupMembership();
/* 356 */     Map<String, String> via = com.google.common.collect.Maps.newHashMap();
/* 357 */     long start = System.currentTimeMillis();
/* 358 */     DistributionList.updateGroupMembership(this.ldapProv, (com.zimbra.cs.ldap.ZLdapContext)null, membership, acct, via, false, false);
/*     */     
/* 360 */     ZimbraLog.test.info("testDLupdateGroupMembershipWithVia %s size=%d via size=%d via=%s", new Object[] { ZimbraLog.elapsedTime(start, System.currentTimeMillis()), Integer.valueOf(membership.groupIds().size()), Integer.valueOf(via.size()), via });
/*     */     
/* 362 */     for (int cnt = 1; cnt <= 8; cnt++) {
/* 363 */       String nam = String.format("normaldl%03d@tgcache.test", new Object[] { Integer.valueOf(cnt) });
/* 364 */       DistributionList dl = this.ldapProv.get(com.zimbra.common.account.Key.DistributionListBy.name, nam);
/* 365 */       Assert.assertTrue(String.format("DL %s (id=%s) in membership", new Object[] { nam, dl.getId() }), membership.groupIds().contains(dl.getId()));
/*     */     }
/*     */     
/* 368 */     DistributionList dl = this.ldapProv.get(com.zimbra.common.account.Key.DistributionListBy.name, "tgdlwithalias@tgcache.test");
/* 369 */     Assert.assertTrue(String.format("DL %s (id=%s) in membership", new Object[] { dl.getId(), dl.getId() }), membership.groupIds().contains(dl.getId()));
/*     */     
/* 371 */     dl = this.ldapProv.get(com.zimbra.common.account.Key.DistributionListBy.name, "tgdlwithalias2@tgcache.test");
/* 372 */     Assert.assertTrue(String.format("DL %s (id=%s) in membership", new Object[] { dl.getId(), dl.getId() }), membership.groupIds().contains(dl.getId()));
/*     */     
/* 374 */     Assert.assertEquals(String.format("Number of DLs User %s is a member of", new Object[] { "tgacctwithalias@tgcache.test" }), 10L, membership.memberOf().size());
/*     */     
/* 376 */     Assert.assertEquals(String.format("Number of vias for User %s", new Object[] { "tgacctwithalias@tgcache.test" }), 1L, via.size());
/*     */     
/* 378 */     acct = this.ldapProv.getAccountByName("tgacctwithalias2@tgcache.test");
/* 379 */     membership = new Provisioning.GroupMembership();
/* 380 */     com.google.common.collect.Maps.newHashMap();
/* 381 */     start = System.currentTimeMillis();
/* 382 */     DistributionList.updateGroupMembership(this.ldapProv, (com.zimbra.cs.ldap.ZLdapContext)null, membership, acct, via, false, false);
/*     */     
/* 384 */     ZimbraLog.test.info("testDLupdateGroupMembershipWithVia %s size=%d via size=%d via=%s", new Object[] { ZimbraLog.elapsedTime(start, System.currentTimeMillis()), Integer.valueOf(membership.groupIds().size()), Integer.valueOf(via.size()), via });
/*     */     
/* 386 */     dl = this.ldapProv.get(com.zimbra.common.account.Key.DistributionListBy.name, "tgdlwithalias@tgcache.test");
/* 387 */     Assert.assertTrue(String.format("DL %s (id=%s) in membership", new Object[] { dl.getName(), dl.getId() }), membership.groupIds().contains(dl.getId()));
/*     */     
/* 389 */     dl = this.ldapProv.get(com.zimbra.common.account.Key.DistributionListBy.name, "tgdlwithalias2@tgcache.test");
/* 390 */     Assert.assertTrue(String.format("DL %s (id=%s) in membership", new Object[] { dl.getName(), dl.getId() }), membership.groupIds().contains(dl.getId()));
/*     */     
/* 392 */     Assert.assertEquals(String.format("Number of DLs User %s is a member of", new Object[] { "tgacctwithalias2@tgcache.test" }), 2L, membership.memberOf().size());
/*     */     
/* 394 */     Assert.assertEquals(String.format("Number of vias for User %s", new Object[] { "tgacctwithalias2@tgcache.test" }), 1L, via.size());
/*     */   }
/*     */   
/*     */   public void testInACLGRoup() throws Exception {
/* 398 */     long start = System.currentTimeMillis();
/*     */     
/* 400 */     String acctName = String.format("person%03d@tgcache.test", new Object[] { Integer.valueOf(1) });
/* 401 */     doInACLGroup(acctName, String.format("normaldl%03d@tgcache.test", new Object[] { Integer.valueOf(1) }), false);
/* 402 */     doInACLGroup(acctName, String.format("dynamicdl%03d@tgcache.test", new Object[] { Integer.valueOf(1) }), false);
/* 403 */     doInACLGroup(acctName, String.format("cosdl%03d@tgcache.test", new Object[] { Integer.valueOf(1) }), false);
/* 404 */     acctName = String.format("person%03d@tgcache.test", new Object[] { Integer.valueOf(2) });
/* 405 */     doInACLGroup(acctName, String.format("normaldl%03d@tgcache.test", new Object[] { Integer.valueOf(1) }), true);
/* 406 */     doInACLGroup(acctName, String.format("dynamicdl%03d@tgcache.test", new Object[] { Integer.valueOf(1) }), false);
/* 407 */     doInACLGroup(acctName, String.format("cosdl%03d@tgcache.test", new Object[] { Integer.valueOf(1) }), false);
/* 408 */     acctName = String.format("person%03d@tgcache.test", new Object[] { Integer.valueOf(3) });
/* 409 */     doInACLGroup(acctName, String.format("normaldl%03d@tgcache.test", new Object[] { Integer.valueOf(1) }), false);
/* 410 */     doInACLGroup(acctName, String.format("dynamicdl%03d@tgcache.test", new Object[] { Integer.valueOf(1) }), true);
/* 411 */     doInACLGroup(acctName, String.format("cosdl%03d@tgcache.test", new Object[] { Integer.valueOf(1) }), false);
/* 412 */     acctName = String.format("person%03d@tgcache.test", new Object[] { Integer.valueOf(4) });
/* 413 */     doInACLGroup(acctName, String.format("normaldl%03d@tgcache.test", new Object[] { Integer.valueOf(1) }), true);
/* 414 */     doInACLGroup(acctName, String.format("dynamicdl%03d@tgcache.test", new Object[] { Integer.valueOf(1) }), false);
/* 415 */     doInACLGroup(acctName, String.format("cosdl%03d@tgcache.test", new Object[] { Integer.valueOf(1) }), true);
/* 416 */     ZimbraLog.test.info("ZZZ testInACLGRoup %s", new Object[] { ZimbraLog.elapsedTime(start, System.currentTimeMillis()) });
/*     */   }
/*     */   
/*     */   private void doInACLGroup(String acctName, String groupName, boolean expected) throws com.zimbra.common.service.ServiceException {
/* 420 */     Account acct = this.ldapProv.getAccountByName(acctName);
/* 421 */     Group group = this.ldapProv.getGroup(com.zimbra.common.account.Key.DistributionListBy.name, groupName);
/* 422 */     assertEquals(String.format("account %s in group %s", new Object[] { acct.getName(), group.getName() }), expected, this.ldapProv.inACLGroup(acct, group.getId()));
/*     */   }
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/* 428 */     this.ldapProv = ((LdapProvisioning)Provisioning.getInstance());
/* 429 */     if (this.soapProv == null) {
/* 430 */       this.soapProv = TestUtil.newSoapProvisioning();
/*     */     }
/* 432 */     tearDown();
/* 433 */     this.domain = createDomain("tgcache.test");
/* 434 */     this.domain1 = createDomain("tgcache1.test");
/* 435 */     this.domain2 = createDomain("tgcache2.test");
/* 436 */     for (int cnt = 1; cnt <= 4; cnt++) {
/* 437 */       createCos(String.format("cdgcachecos%03d", new Object[] { Integer.valueOf(cnt) }));
/*     */     }
/* 439 */     this.groups.clear();
/* 440 */     for (int cnt = 1; cnt <= 4; cnt++) {
/* 441 */       TestUtil.deleteAccount(String.format("cosdl%03d@tgcache.test", new Object[] { Integer.valueOf(cnt) }));
/* 442 */       Group grp = createCustomDynamicGroupWhoseMembersShareCOS(String.format("cosdl%03d@tgcache.test", new Object[] { Integer.valueOf(cnt) }), String.format("cdgcachecos%03d", new Object[] { Integer.valueOf(cnt) }));
/* 443 */       this.groups.put(grp.getId(), grp);
/*     */     }
/* 445 */     List<String> staticMembers = com.google.common.collect.Lists.newArrayList();
/* 446 */     List<String> dynamicMembers = com.google.common.collect.Lists.newArrayList();
/* 447 */     for (int cnt = 1; cnt <= 8; cnt++) {
/* 448 */       int cosChoice = cnt % 4 + 1;
/* 449 */       createAccountAsMemberOfCOS(String.format("person%03d@tgcache.test", new Object[] { Integer.valueOf(cnt) }), String.format("cdgcachecos%03d", new Object[] { Integer.valueOf(cosChoice) }));
/* 450 */       if (cnt % 2 == 0) {
/* 451 */         staticMembers.add(String.format("person%03d@tgcache.test", new Object[] { Integer.valueOf(cnt) }));
/*     */       }
/* 453 */       if (cnt % 3 == 0) {
/* 454 */         dynamicMembers.add(String.format("person%03d@tgcache.test", new Object[] { Integer.valueOf(cnt) }));
/*     */       }
/*     */     }
/* 457 */     createAccountWithAlias("tgacctwithalias@tgcache.test", "tgacctalias@tgcache.test");
/* 458 */     createAccountWithAlias("tgacctwithalias2@tgcache.test", "tgacctalias2@tgcache.test");
/* 459 */     staticMembers.add("tgacctalias@tgcache.test");
/* 460 */     DistributionList dlalias = createDistributionList("tgdlwithalias@tgcache.test");
/* 461 */     dlalias.addAlias("tgdlalias@tgcache.test");
/* 462 */     DistributionList dlalias2 = createDistributionList("tgdlwithalias2@tgcache.test");
/* 463 */     dlalias2.addAlias("tgdlalias2@tgcache.test");
/* 464 */     String[] aliasmems = { "tgdlalias2@tgcache.test" };
/* 465 */     dlalias.addMembers(aliasmems);
/* 466 */     String[] aliasmems2 = { "tgacctalias@tgcache.test", "tgacctalias2@tgcache.test" };
/* 467 */     dlalias2.addMembers(aliasmems2);
/* 468 */     DistributionList dl; for (int cnt = 1; cnt <= 8; cnt++) {
/* 469 */       dl = createDistributionList(String.format("normaldl%03d@tgcache.test", new Object[] { Integer.valueOf(cnt) }));
/* 470 */       this.groups.put(dl.getId(), dl);
/* 471 */       dl.addMembers((String[])staticMembers.toArray(new String[staticMembers.size()]));
/* 472 */       for (String mem : staticMembers)
/* 473 */         ZimbraLog.test.info("SETUP Distribution List %s has member acct=%s", new Object[] { dl.getName(), mem });
/*     */     }
/*     */     Group grp;
/* 476 */     for (int cnt = 1; cnt <= 8; cnt++) {
/* 477 */       grp = createDynamicGroup(String.format("dynamicdl%03d@tgcache.test", new Object[] { Integer.valueOf(cnt) }));
/* 478 */       this.groups.put(grp.getId(), grp);
/* 479 */       this.ldapProv.addGroupMembers(grp, (String[])dynamicMembers.toArray(new String[dynamicMembers.size()]));
/* 480 */       for (String mem : dynamicMembers) {
/* 481 */         ZimbraLog.test.info("SETUP Dynamic Group %s has member acct=%s", new Object[] { grp.getName(), mem });
/*     */       }
/*     */     }
/* 484 */     String groupName = String.format("cosdl%03d@tgcache.test", new Object[] { Integer.valueOf(1) });
/*     */     
/*     */ 
/*     */ 
/* 488 */     this.ldapProv.grantRight("domain", com.zimbra.soap.type.TargetBy.name, this.domain.getName(), "grp", com.zimbra.soap.admin.type.GranteeSelector.GranteeBy.name, groupName, null, "createDistList", null);
/*     */     
/*     */ 
/* 491 */     ZimbraLog.test.info("SETUP Granted %s to Group %s for dom=%s", new Object[] { "createDistList", groupName, this.domain.getName() });
/*     */     
/*     */ 
/*     */ 
/* 495 */     groupName = String.format("normaldl%03d@tgcache.test", new Object[] { Integer.valueOf(1) });
/* 496 */     this.ldapProv.grantRight("domain", com.zimbra.soap.type.TargetBy.name, this.domain1.getName(), "grp", com.zimbra.soap.admin.type.GranteeSelector.GranteeBy.name, groupName, null, "createDistList", null);
/*     */     
/*     */ 
/* 499 */     ZimbraLog.test.info("SETUP Granted %s to Group %s for dom=%s", new Object[] { "createDistList", groupName, this.domain.getName() });
/* 500 */     this.ldapProv.grantRight("domain", com.zimbra.soap.type.TargetBy.name, this.domain.getName(), "grp", com.zimbra.soap.admin.type.GranteeSelector.GranteeBy.name, groupName, null, "sendToDistList", null);
/*     */     
/*     */ 
/* 503 */     ZimbraLog.test.info("SETUP Granted %s to Group %s for dom=%s", new Object[] { "sendToDistList", groupName, this.domain.getName() });
/*     */     
/*     */ 
/*     */ 
/* 507 */     groupName = String.format("dynamicdl%03d@tgcache.test", new Object[] { Integer.valueOf(1) });
/* 508 */     this.ldapProv.grantRight("domain", com.zimbra.soap.type.TargetBy.name, this.domain2.getName(), "grp", com.zimbra.soap.admin.type.GranteeSelector.GranteeBy.name, groupName, null, "createDistList", null);
/*     */     
/*     */ 
/* 511 */     ZimbraLog.test.info("SETUP Granted %s to Group %s for dom=%s", new Object[] { "createDistList", groupName, this.domain.getName() });
/*     */     
/*     */ 
/*     */ 
/* 515 */     groupName = String.format("dynamicdl%03d@tgcache.test", new Object[] { Integer.valueOf(2) });
/* 516 */     this.ldapProv.grantRight("domain", com.zimbra.soap.type.TargetBy.name, this.domain2.getName(), "grp", com.zimbra.soap.admin.type.GranteeSelector.GranteeBy.name, groupName, null, "sendToDistList", null);
/*     */     
/*     */ 
/* 519 */     ZimbraLog.test.info("SETUP Granted %s to Group %s for dom=%s", new Object[] { "sendToDistList", groupName, this.domain.getName() });
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception
/*     */   {
/* 524 */     deleteGroupIfExists("tgdlwithalias@tgcache.test");
/* 525 */     deleteGroupIfExists("tgdlwithalias2@tgcache.test");
/* 526 */     for (int cnt = 1; cnt <= 8; cnt++) {
/* 527 */       deleteGroupIfExists(String.format("normaldl%03d@tgcache.test", new Object[] { Integer.valueOf(cnt) }));
/*     */     }
/* 529 */     for (int cnt = 1; cnt <= 8; cnt++) {
/* 530 */       deleteGroupIfExists(String.format("dynamicdl%03d@tgcache.test", new Object[] { Integer.valueOf(cnt) }));
/*     */     }
/* 532 */     for (int cnt = 1; cnt <= 4; cnt++) {
/* 533 */       deleteGroupIfExists(String.format("cosdl%03d@tgcache.test", new Object[] { Integer.valueOf(cnt) }));
/*     */     }
/* 535 */     TestUtil.deleteAccount("tgacctwithalias@tgcache.test");
/* 536 */     TestUtil.deleteAccount("tgacctwithalias2@tgcache.test");
/* 537 */     for (int cnt = 1; cnt <= 8; cnt++) {
/* 538 */       TestUtil.deleteAccount(String.format("person%03d@tgcache.test", new Object[] { Integer.valueOf(cnt) }));
/*     */     }
/* 540 */     for (int cnt = 1; cnt <= 4; cnt++) {
/* 541 */       deleteCosIfExists(String.format("cdgcachecos%03d", new Object[] { Integer.valueOf(cnt) }));
/*     */     }
/* 543 */     if (this.domain != null) {
/* 544 */       this.soapProv.deleteDomain(this.domain.getId());
/* 545 */       this.domain = null;
/*     */     }
/* 547 */     if (this.domain1 != null) {
/* 548 */       this.soapProv.deleteDomain(this.domain1.getId());
/* 549 */       this.domain1 = null;
/*     */     }
/* 551 */     if (this.domain2 != null) {
/* 552 */       this.soapProv.deleteDomain(this.domain2.getId());
/* 553 */       this.domain2 = null;
/*     */     }
/*     */   }
/*     */   
/*     */   public Account createAccountWithAlias(String name, String alias) throws Exception
/*     */   {
/* 559 */     Provisioning prov = Provisioning.getInstance();
/* 560 */     Account acct = prov.get(com.zimbra.common.account.Key.AccountBy.name, name);
/* 561 */     if (acct == null) {
/* 562 */       Map<String, Object> attrs = com.google.common.collect.Maps.newHashMap();
/* 563 */       acct = prov.createAccount(name, "test123", attrs);
/*     */     } else {
/* 565 */       ZimbraLog.test.warn("createAccountWithAlias(%s) - already existed!!!", new Object[] { name });
/*     */     }
/* 567 */     if (acct == null) {
/* 568 */       ZimbraLog.test.warn("createAccountWithAliase(%s) returning null!!!", new Object[] { name });
/*     */     }
/* 570 */     prov.addAlias(acct, alias);
/* 571 */     return acct;
/*     */   }
/*     */   
/*     */   public Account createAccountAsMemberOfCOS(String name, String cosName) throws Exception
/*     */   {
/* 576 */     Provisioning prov = Provisioning.getInstance();
/* 577 */     Account acct = prov.get(com.zimbra.common.account.Key.AccountBy.name, name);
/* 578 */     if (acct != null) {
/* 579 */       ZimbraLog.test.warn("createAccountAsMemberOfCOS(%s,%s) - already existed!!!", new Object[] { name, cosName });
/* 580 */       return acct;
/*     */     }
/* 582 */     Map<String, Object> attrs = com.google.common.collect.Maps.newHashMap();
/* 583 */     com.zimbra.cs.account.Cos cos = prov.get(com.zimbra.common.account.Key.CosBy.name, cosName);
/* 584 */     attrs.put("zimbraCOSId", cos.getId());
/* 585 */     acct = prov.createAccount(name, "test123", attrs);
/* 586 */     if (acct == null) {
/* 587 */       ZimbraLog.test.warn("createAccountAsMemberOfCOS(%s,%s) returning null!!!", new Object[] { name, cosName });
/*     */     }
/* 589 */     return acct;
/*     */   }
/*     */   
/*     */   public static Domain createDomain(String name) throws Exception
/*     */   {
/* 594 */     Provisioning prov = Provisioning.getInstance();
/* 595 */     Domain dom = prov.get(com.zimbra.common.account.Key.DomainBy.name, name);
/* 596 */     if (dom != null) {
/* 597 */       ZimbraLog.test.warn("createDomain(%s) - already existed!!!", new Object[] { name });
/* 598 */       return dom;
/*     */     }
/* 600 */     Map<String, Object> attrs = com.google.common.collect.Maps.newHashMap();
/* 601 */     dom = prov.createDomain(name, attrs);
/* 602 */     if (dom == null) {
/* 603 */       ZimbraLog.test.warn("createDomain returning null for '%s'", new Object[] { name });
/*     */     }
/* 605 */     return dom;
/*     */   }
/*     */   
/*     */   public static com.zimbra.cs.account.Cos createCos(String name) throws Exception
/*     */   {
/* 610 */     Provisioning prov = Provisioning.getInstance();
/* 611 */     com.zimbra.cs.account.Cos cos = prov.get(com.zimbra.common.account.Key.CosBy.name, name);
/* 612 */     if (cos != null) {
/* 613 */       ZimbraLog.test.warn("createCos(%s) - already existed!!!", new Object[] { name });
/* 614 */       return cos;
/*     */     }
/* 616 */     cos = prov.createCos(name, null);
/* 617 */     if (cos == null) {
/* 618 */       ZimbraLog.test.warn("createCos returning null for '%s'", new Object[] { name });
/*     */     }
/* 620 */     return cos;
/*     */   }
/*     */   
/*     */   public static Group createDynamicGroup(String name) throws Exception
/*     */   {
/* 625 */     Provisioning prov = Provisioning.getInstance();
/* 626 */     Group group = prov.getGroup(com.zimbra.common.account.Key.DistributionListBy.name, name, true);
/* 627 */     if (group != null) {
/* 628 */       ZimbraLog.test.warn("createDynamicGroup(%s) - already existed!!!", new Object[] { name });
/* 629 */       return group;
/*     */     }
/* 631 */     Map<String, Object> attrs = com.google.common.collect.Maps.newHashMap();
/* 632 */     group = prov.createGroup(name, attrs, true);
/* 633 */     if (group == null) {
/* 634 */       ZimbraLog.test.warn("createDynamicGroup returning null for '%s'", new Object[] { name });
/*     */     }
/* 636 */     return group;
/*     */   }
/*     */   
/*     */   public static Group createCustomDynamicGroupWhoseMembersShareCOS(String name, String cosName) throws Exception
/*     */   {
/* 641 */     Provisioning prov = Provisioning.getInstance();
/* 642 */     String dName = name.substring(0, name.indexOf('@') - 1);
/* 643 */     Group group = prov.getGroup(com.zimbra.common.account.Key.DistributionListBy.name, name, true);
/* 644 */     if (group != null) {
/* 645 */       ZimbraLog.test.warn("createCustomDynamicGroupWhoseMembersShareCOS(%s) - already existed!!!", new Object[] { name });
/* 646 */       return group;
/*     */     }
/* 648 */     Map<String, Object> attrs = com.google.common.collect.Maps.newHashMap();
/* 649 */     com.zimbra.cs.account.Cos cos = prov.get(com.zimbra.common.account.Key.CosBy.name, cosName);
/* 650 */     attrs.put("memberURL", String.format("ldap:///??sub?(&(objectClass=zimbraAccount)(zimbraCOSId=%s)(zimbraAccountStatus=active))", new Object[] { cos.getId() }));
/*     */     
/*     */ 
/*     */ 
/* 654 */     attrs.put("zimbraIsACLGroup", "TRUE");
/* 655 */     attrs.put("zimbraMailStatus", "enabled");
/* 656 */     attrs.put("displayName", dName);
/* 657 */     group = prov.createGroup(name, attrs, true);
/* 658 */     if (group == null) {
/* 659 */       ZimbraLog.test.debug("ensureCustomDynamicGroupExists returning null for '%s'", new Object[] { name });
/*     */     }
/* 661 */     return group;
/*     */   }
/*     */   
/*     */   public static DistributionList createDistributionList(String name) throws Exception
/*     */   {
/* 666 */     Provisioning prov = Provisioning.getInstance();
/* 667 */     DistributionList dl = prov.get(com.zimbra.common.account.Key.DistributionListBy.name, name);
/* 668 */     if (dl != null) {
/* 669 */       ZimbraLog.test.warn("createDistributionList(%s) - already existed!!!", new Object[] { name });
/* 670 */       return dl;
/*     */     }
/* 672 */     Map<String, Object> attrs = com.google.common.collect.Maps.newHashMap();
/* 673 */     dl = prov.createDistributionList(name, attrs);
/* 674 */     if (dl == null) {
/* 675 */       ZimbraLog.test.debug("createDistributionList returning null for '%s'", new Object[] { name });
/*     */     }
/* 677 */     return dl;
/*     */   }
/*     */   
/*     */   public static void deleteGroupIfExists(String name) {
/*     */     try {
/* 682 */       Provisioning prov = Provisioning.getInstance();
/* 683 */       Group group = prov.getGroup(com.zimbra.common.account.Key.DistributionListBy.name, name, true);
/* 684 */       if (group != null) {
/* 685 */         prov.deleteGroup(group.getId());
/*     */       }
/*     */     } catch (Exception ex) {
/* 688 */       ZimbraLog.test.error("Problem deleting group " + name, ex);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void deleteCosIfExists(String name) {
/*     */     try {
/* 694 */       Provisioning prov = Provisioning.getInstance();
/* 695 */       com.zimbra.cs.account.Cos res = prov.get(com.zimbra.common.account.Key.CosBy.name, name);
/* 696 */       if (res != null) {
/* 697 */         prov.deleteCos(res.getId());
/*     */       }
/*     */     } catch (Exception ex) {
/* 700 */       ZimbraLog.test.error("Problem deleting Cos " + name, ex);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args)
/*     */     throws Exception
/*     */   {
/*     */     
/*     */     try
/*     */     {
/* 710 */       TestUtil.runTest(TestGroups.class);
/*     */     } catch (Exception e) {
/* 712 */       System.out.println(e.getMessage());
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestGroups.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */